from alive_progress import alive_bar
from progress.bar import IncrementalBar
from progressbar import *
import sys
import time
from tqdm import tqdm
import PySimpleGUI as sg


# 进度条
# demo1
def process_bar(percent, start_str='', end_str='', total_length=0):
    bar = ''.join(["\033[31m%s\033[0m" % '   '] *
                  int(percent * total_length)) + ''
    bar = '\r' + start_str + \
        bar.ljust(total_length) + ' {:0>4.1f}%|'.format(percent*100) + end_str
    print(bar, end='', flush=True)


for i in range(101):
    time.sleep(0.1)
    end_str = '100%'
    process_bar(i/100, start_str='', end_str=end_str, total_length=15)


# demo2
for i in range(0, 101, 2):
    time.sleep(0.1)
    num = i // 2
    if i == 100:
        process = "\r[%3s%%]: |%-50s|\n" % (i, '|' * num)
    else:
        process = "\r[%3s%%]: |%-50s|" % (i, '|' * num)
    print(process, end='', flush=True)

# demo3
print("正在下载......")
for i in range(11):
    if i != 10:
        # sys.stdout.write()打印不换行
        sys.stdout.write("==")
    else:
        sys.stdout.write("== " + str(i*10)+"%/100%")
    # sys.stdout.flush()方法是立即刷新输出的内容
    sys.stdout.flush()
    time.sleep(0.2)
print("\n" + "下载完成")


# demo4
for i in tqdm(range(20)):
    time.sleep(0.5)

# demo5
progress = ProgressBar()
for i in progress(range(1000)):
    time.sleep(0.01)


# demo6
for i in range(1, 101):
    print("\r", end="")
    print("Download progress: {}%: ".format(i), "▋" * (i // 2), end="")
    sys.stdout.flush()
    time.sleep(0.05)

# demo7
scale = 50
print("执行开始，祈祷不报错".center(scale // 2, "-"))
start = time.perf_counter()
for i in range(scale + 1):
    a = "*" * i
    b = "." * (scale - i)
    c = (i / scale) * 100
    dur = time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur), end="")
    time.sleep(0.1)
print("\n"+"执行结束，万幸".center(scale // 2, "-"))

# demo8
# 这里同样的，tqdm就是这个进度条最常用的一个方法
# 里面存一个可迭代对象
for i in tqdm(range(1, 500)):
    # 模拟你的任务
    time.sleep(0.01)
time.sleep(0.5)


# demo9
mylist = [1, 2, 3, 4, 5, 6, 7, 8]
bar = IncrementalBar('Countdown', max=len(mylist))
for item in mylist:
    bar.next()
    time.sleep(1)
    bar.finish()


# demo10
# retrieve your set of items
items = range(100)
# declare your expected total
with alive_bar(len(items)) as bar:
    # iterate as usual
    for item in items:
        # process each item
        bar()
        time.sleep(0.1)


# demo11
mylist = [1, 2, 3, 4, 5, 6, 7, 8]
for i, item in enumerate(mylist):
    sg.one_line_progress_meter(
        'This is my progress meter!', i+1, len(mylist), '-key-')
    time.sleep(1)
